<?php

namespace App\Jobs\Middleware;

use Closure;
use Exception;
use Throwable;
use App\Models\Admin\Supplier;
use Illuminate\Support\Facades\Log;


class SupplierDbConnectCheck
{
    /**
     * @param Supplier $supplier
     */
    public function __construct(public Supplier $supplier)
    {
    }

    /**
     * @param object $job
     * @param Closure $next
     * @return void
     */
    public function handle(object $job, Closure $next): void
    {
        try {
            if (!$this->supplier->registerDbConnect()) {
                throw new Exception("供应商注册数据库配置失败");
            }
            $next($job);
        } catch (Throwable $e) {
            $job->fail($e);
            Log::channel('error')->error('供应商配置出现异常', [
                'supplier'  => $this->supplier->toArray(),
                'error'     => error2array($e),
            ]);
        }
    }
}
